package com.lrkj.payfee.service.impl;

import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.payfee.domain.ChargeItem;
import com.lrkj.payfee.mapper.ChargeItemMapper;
import com.lrkj.payfee.service.IChargeItemService;
import com.lrkj.payfee.service.IChargeProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收费项目Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-26
 */
@Service
public class ChargeItemServiceImpl implements IChargeItemService {
    @Autowired
    private ChargeItemMapper chargeItemMapper;
    @Autowired
    private IChargeProjectService chargeProjectService;

    /**
     * 查询收费项目
     *
     * @param chargeId 收费项目ID
     * @return 收费项目
     */
    @Override
    public ChargeItem selectChargeItemById(String chargeId) {
        return chargeItemMapper.selectChargeItemById(chargeId);
    }

    /**
     * 查询收费项目列表
     *
     * @param chargeItem 收费项目
     * @return 收费项目
     */
    @Override
    public List<ChargeItem> selectChargeItemList(ChargeItem chargeItem) {
        return chargeItemMapper.selectChargeItemList(chargeItem);
    }

    /**
     * 新增收费项目
     *
     * @param chargeItem 收费项目
     * @return 结果
     */
    @Override
    public int insertChargeItem(ChargeItem chargeItem) {
        int rows = 0;
        try {
            chargeItem.setChargeId(chargeItem.getChargeCode());
            chargeItem.setCreateBy(SecurityUtils.getUsername());
            chargeItem.setCreateTime(DateUtils.getNowDate());
            rows = chargeItemMapper.insertChargeItem(chargeItem);
        } catch (Exception e) {
            if(e.getMessage().indexOf("for key 'PRIMARY'") != -1){
                throw new BaseException("代码已存在！");
            }
        }
        return rows;
    }

    /**
     * 修改收费项目
     *
     * @param chargeItem 收费项目
     * @return 结果
     */
    @Override
    public int updateChargeItem(ChargeItem chargeItem) {
        chargeItem.setUpdateBy(SecurityUtils.getUsername());
        chargeItem.setUpdateTime(DateUtils.getNowDate());
        return chargeItemMapper.updateChargeItem(chargeItem);
    }

    /**
     * 批量删除收费项目
     *
     * @param chargeIds 需要删除的收费项目ID
     * @return 结果
     */
    @Override
    public int deleteChargeItemByIds(String[] chargeIds) {
        return chargeItemMapper.deleteChargeItemByIds(chargeIds);
    }

    /**
     * 删除收费项目信息
     *
     * @param chargeId 收费项目ID
     * @return 结果
     */
    @Override
    public int deleteChargeItemById(String chargeId) {
        return chargeItemMapper.deleteChargeItemById(chargeId);
    }

    @Override
    public ChargeItem selectItemByKindId(String instrumentKind) {
        return chargeItemMapper.selectItemByKindId(instrumentKind);
    }
}
